package com.mindainfo.configCenter.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.serializer.ValueFilter;
import org.bson.types.ObjectId;

/**
 * Created by MD-01 on 2016/11/16.
 */
public class JSONUtil {

    public static String toJson(Object o){

        SerializerFeature[] features = {
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteDateUseDateFormat
        };

        ValueFilter filter = new ValueFilter(){

            @Override
            public Object process(Object object, String name, Object value) {

                if(name.equals("id")&&value!=null&&value instanceof ObjectId){
                    return value.toString();
                }
                return value;
            }
        };

        return JSON.toJSONString(o,filter,features);
    }
}
